<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\helper;

final class Options
{
    public function parse($from)
    {
        if (strpos($from, '://') === false) {
            return $this->_parse_content($from);
        }

        $components = parse_url(trim($from, "()"));
        if ($components['query']) {
            parse_str($components['query'], $components['query']);
        } else {
            $components['query'] = array();
        }

        $method = '_parse_scheme_' . $components['scheme'];
        return $this->$method($components);
    }

    private function _parse_scheme_config($components)
    {
        return (array) config($components['host']);
    }

    private function _parse_scheme_options($components)
    {
        return model('admin/option')->get_options_array($components['host']);
    }

    private function _parse_scheme_model($components)
    {
        if (isset($components['query']['method'])) {
            $args = explode(',', isset($components['query']['args']) ? $components['query']['args'] : '');
            $callback = array(model($components['path']), $components['query']['method']);
            return (array) call_user_func_array($callback, $args);
        }

        $scope = array_map('trim', explode(',', isset($components['query']['scope']) ? $components['query']['scope'] : ''));
        $field = $components['query']['field'];
        $order = isset($components['query']['order']) ? $components['query']['order'] : '';

        $model = model($components['host'] . $components['path']);

        if ($scope) {
            $model = $model->scope($scope);
        }

        if ($order) {
            $model->order($order);
        }

        $rows = $model->field($field)->select();
        $options = [];

        foreach ($rows as $row) {
            $row = $row->getData();
            $value = array_shift($row);
            $title = $row ? array_shift($row) : $value;
            $options[$value] = $title;
        }

        return $options;
    }

    private function _parse_scheme_service($components)
    {
        $args = explode(',', isset($components['query']['args']) ? $components['query']['args'] : '');
        $callback = array(service($components['path']), $components['query']['method']);
        return (array) call_user_func_array($callback, $args);
    }

    private function _parse_content($content)
    {
        // $content格式："1:1\r\n2:3"
        $array = preg_split('/[\r\n]+/', trim($content, "\r\n"));
        if (strpos($content, ':')) {
            $list = array();
            foreach ($array as $val) {
                list($k, $v) = array_map('trim', explode(':', $val));

                if (strpos($v, ',')) {
                    $v = array_map('trim', explode(',', $v));
                }

                $list[$k]   = $v;
            }
            return $list;
        } else {
            return array_map('trim', $array);
        }
    }
}
